//
//  YXYOmnibusTableViewCell.m
//  Topit
//
//  Created by dllo on 15/12/10.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "YXYOmnibusTableViewCell.h"

@implementation YXYOmnibusTableViewCell
- (void)dealloc {
    [_myLabel release];
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.number = [NSMutableArray arrayWithCapacity:0];
        
        for (int i = 0; i < 10; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
            imageView.tag = 1000 + i;
            [self addSubview:imageView];
            imageView.userInteractionEnabled = YES;
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            [button addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = 600 + i;
            [imageView addSubview:button];
            
            UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectZero];
            myLabel.font = [UIFont systemFontOfSize:12];
            [self addSubview:myLabel];
            myLabel.tag = 2000 + i;
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
            label.font = [UIFont systemFontOfSize:10];
            label.textColor = [UIColor grayColor];
            [self addSubview:label];
            label.tag = 3000 + i;
        }
    }
    return self;
}


- (void)setMyArray:(NSMutableArray *)myArray {
    if (_myArray != myArray) {
        [_myArray release];
        _myArray = [myArray retain];
    }
    NSMutableArray *idName = [NSMutableArray arrayWithCapacity:0];
    NSMutableArray *bioArray = [NSMutableArray arrayWithCapacity:0];
    NSMutableArray *arrayURl = [NSMutableArray arrayWithCapacity:0];

    self.num = 0;
    for (int i = 1; i < myArray.count; i++) {
        YXYWallPaperModel *paperModel = myArray[i];
        for (int i = 0; i < paperModel.itemArray.count; i++) {
            YXYWallItemModel *itemModel = paperModel.itemArray[i];
            [idName addObject:itemModel.name];
            [bioArray addObject:itemModel.bio];
            [arrayURl addObject:itemModel.icon[@"url"]];
            self.num += 1;
        }
    }
   
    for (int i = 0; i < _num; i++) {
        UIImageView *image = (UIImageView *)[self viewWithTag:1000 + i];
        [image sd_setImageWithURL:[NSURL URLWithString:arrayURl[i]]];
        
        UILabel *name = (UILabel *)[self viewWithTag:2000 + i];
        name.text = idName[i];
        
        UILabel *bio = (UILabel *)[self viewWithTag:3000 + i];
        bio.text = bioArray[i];
        
    }
}

-(void)layoutSubviews {
    [super layoutSubviews];
    CGFloat kOriginy = 0.0;
    CGFloat kImageW = self.width / 2;
    NSLog(@"%f", self.height);
    
    for (int i = 0; i < _num - 2; i++) {
        UIImageView *imageView = (UIImageView *)[self viewWithTag:1000 + i];
        imageView.frame = CGRectMake(5 + kImageW * (i % 2), 200 * (i / 2), kImageW - 10, 160);
        
        UIButton *button = (UIButton *)[imageView viewWithTag:600 + i];
        button.frame = CGRectMake(0, 0, imageView.width, imageView.height);
        
        UILabel *name = (UILabel *)[self viewWithTag:2000 + i];
        name.frame = CGRectMake(imageView.origin.x, imageView.origin.y + imageView.height + 10, imageView.width, 10);
        
        UILabel *bio = (UILabel *)[self viewWithTag:3000 + i];
        bio.frame = CGRectMake(name.origin.x, name.origin.y + name.height + 10, name.width, 10);
        kOriginy = imageView.height + name.height + bio.height;
    }
}

- (void)buttonDidPress:(UIButton *)sender {
    NSLog(@"1");
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
